﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LAVAA.DAL;
using LAVAA.BL.Business;


namespace LAVAA.BL.Control
{
    /// <summary>
    /// Fasadklass, så lite logik som möjligt här
    /// Konvertering till/från generell businesslogik, olika mellan olika typer av klienter
    /// </summary>
    public class UserCase01
    {
        UnitOfWork _uow = new UnitOfWork();
        public void AddActivity(Activity activity, Guid activityTypeId, Guid userId)
        {
            ActivityManager activityMgr = new ActivityManager(_uow);

            var activityType = _uow.ActivityTypeRepository.GetById(activityTypeId);
            var user = _uow.UserRepository.GetById(userId);

            activity.ActivityType = activityType;
            activity.User = user;

            activityMgr.AddActivity(activity);
            
            _uow.Commit();
        }

        public Activity FindActivityById(Guid id)
        {
            return _uow.ActivityRepository.GetById(id);
        }

        public void UpdateActivity(Activity activity)
        {
            ActivityManager activityMgr = new ActivityManager(_uow);
            activityMgr.UpdateActivity(activity);
        }

        public User GetUser(string username)
        {
            User user = _uow.UserRepository.Find(filter: u => u.UserId == username).FirstOrDefault();
            return user;
        }

        public IEnumerable<ActivityType> FindActivityTypes()
        {
            return _uow.ActivityTypeRepository.GetAll();
        }

        public IEnumerable<Activity> FindActivities(string username)
        {
            return _uow.ActivityRepository.Find(
                filter: a => a.User.UserId.Equals(username),
                orderBy: q => q.OrderBy(d => d.Date));
        }

        public IEnumerable<Activity> FindActivitiesByTypeName(string activityTypeName)
        {
            return _uow.ActivityRepository.Find(
                filter: a => a.ActivityType.Name.Contains(activityTypeName), 
                orderBy: q => q.OrderBy(d => d.Date));
        }

        public void CreateActivityType(ActivityType activityType)
        {
            if (_uow.ActivityTypeRepository.Find(t => t.Name == activityType.Name).Count() == 0)
            {
                _uow.ActivityTypeRepository.Add(activityType);
                _uow.Commit();
            }
            else
                throw new Exception("Aktivitetstyp finns redan!");
        }

        public void UpdateActivityType(ActivityType activityType)
        {
            if (_uow.ActivityTypeRepository.Find(t => t.Name == activityType.Name).Count() == 0)
            {
                _uow.ActivityTypeRepository.Update(activityType);
                _uow.Commit();
            }
            else
                throw new Exception("Aktivitetstyp finns redan!");
        }

        public void DeleteActivityType(Guid id)
        {
            if (_uow.ActivityTypeRepository.Find(t => t.Id == id).Count() == 1)
            {
                _uow.ActivityTypeRepository.Delete(id);
                _uow.Commit();
            }
            else
                throw new Exception("Aktivitetstyp finns inte!");
        }

        public ActivityType FindActivityTypesById(Guid id)
        {
            return _uow.ActivityTypeRepository.GetById(id);
        }
    }
}
